package com.imooc.controller;

import com.imooc.pojo.Items;
import com.imooc.pojo.ItemsImg;
import com.imooc.pojo.ItemsParam;
import com.imooc.pojo.ItemsSpec;
import com.imooc.pojo.vo.CommentLevelCountsVO;
import com.imooc.pojo.vo.ItemInfoVO;
import com.imooc.service.ItemService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author laijun
 * @version 1.0
 * @date 2021/4/8 上午10:18
 */
@Api(value = "详情页接口", tags = {"详情页接口"})
@RequestMapping("/items")
@RestController
public class ItemController {
    @Autowired
    ItemService itemService;

    @ApiOperation(value = "渲染商品信息", notes = "渲染商品信息", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public IMOOCJSONResult sixNewItems(@PathVariable("itemId") String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return IMOOCJSONResult.errorMsg("分类不存在");
        }

//        查询数据
        Items items = itemService.queryItemById(itemId);
        List<ItemsImg> itemsImgs = itemService.queryItemImgList(itemId);
        List<ItemsSpec> itemsSpecs = itemService.queryItemSpecList(itemId);
        ItemsParam itemsParam = itemService.queryItemParam(itemId);

        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(items);
        itemInfoVO.setItemImgList(itemsImgs);
        itemInfoVO.setItemSpecList(itemsSpecs);
        itemInfoVO.setItemParams(itemsParam);

        return IMOOCJSONResult.ok(itemInfoVO);
    }

    @ApiOperation(value = "渲染商品评价等级数量", notes = "渲染商品评价等级数量", httpMethod = "GET")
    @GetMapping("/commentLevel")
    public IMOOCJSONResult commentLevel(@RequestParam String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return IMOOCJSONResult.errorMsg(null);
        }
        CommentLevelCountsVO commentLevelCountsVO = itemService.queryCommentCounts(itemId);

        return IMOOCJSONResult.ok(commentLevelCountsVO);
    }

    @ApiOperation(value = "根据评论等级进行查询", notes = "根据评论等级进行查询", httpMethod = "GET")
    @GetMapping("/comments")
    public IMOOCJSONResult commentLevel(
            @ApiParam(name = "itemId", value = "商品Id", required = true)
            @RequestParam
                    String itemId,
            @ApiParam(name = "level", value = "评价等级", required = false)
            @RequestParam
                    Integer level,
            @ApiParam(name = "page", value = "第几页", example = "1", required = false)
            @RequestParam
                    Integer page,
            @ApiParam(name = "pageSize", value = "分页条目数量", example = "20", required = false)
            @RequestParam
                    Integer pageSize) {
        if (StringUtils.isBlank(itemId)) {
            return IMOOCJSONResult.errorMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = 20;
        }

        PagedGridResult result = itemService.queryPagedComments(itemId, level, page, pageSize);

        return IMOOCJSONResult.ok(result);
    }

    @ApiOperation(value = "根据搜索查询商品", notes = "根据搜索查询商品", httpMethod = "GET")
    @GetMapping("/search")
    public IMOOCJSONResult search(
            @ApiParam(name = "keywords", value = "关键字", required = true)
            @RequestParam
                    String keywords,
            @ApiParam(name = "sort", value = "排序", required = false)
            @RequestParam
                    String sort,
            @ApiParam(name = "page", value = "第几页", example = "1", required = false)
            @RequestParam
                    Integer page,
            @ApiParam(name = "pageSize", value = "分页条目数量", example = "20", required = false)
            @RequestParam
                    Integer pageSize) {
        if (StringUtils.isBlank(keywords)) {
            return IMOOCJSONResult.errorMsg(null);
        }

        if (StringUtils.isBlank(sort)) {
            sort = "k";
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = 20;
        }
        PagedGridResult result = itemService.searchItems(keywords, sort, page, pageSize);

        return IMOOCJSONResult.ok(result);
    }

    @ApiOperation(value = "根据三级分类查询商品", notes = "根据三级分类查询商品", httpMethod = "GET")
    @GetMapping("/catItems")
    public IMOOCJSONResult catItems(
            @ApiParam(name = "catId", value = "三级分类ID", required = true)
            @RequestParam
                    Integer catId,
            @ApiParam(name = "sort", value = "排序", required = false)
            @RequestParam
                    String sort,
            @ApiParam(name = "page", value = "第几页", example = "1", required = false)
            @RequestParam
                    Integer page,
            @ApiParam(name = "pageSize", value = "分页条目数量", example = "20", required = false)
            @RequestParam
                    Integer pageSize) {
        if (catId == null) {
            return IMOOCJSONResult.errorMsg("分类不存在");

        }
        if (StringUtils.isBlank(sort)) {
            sort = "k";
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = 20;
        }
        PagedGridResult result = itemService.searchItemsByCatId(catId, sort, page, pageSize);

        return IMOOCJSONResult.ok(result);
    }


    @GetMapping("/refresh")
    public IMOOCJSONResult refresh(@RequestParam String itemSpecIds) {
        if (StringUtils.isBlank(itemSpecIds)) {
            return IMOOCJSONResult.ok();
        }
        return IMOOCJSONResult.ok(itemService.queryItemsBySpecIds(itemSpecIds));
    }

}
